πŸ‘©β€πŸ’» 42888. μ˜€ν”ˆμ±„νŒ…λ°©

@Seongukbaek Posted June 10, 2022 Β· Updated June 10, 2022 Β· 2 min read

문제

[Programmers - μ˜€ν”ˆμ±„νŒ…λ°©](https://programmers.co.kr/learn/courses/30/lessons/42888

πŸ“ Logic

Map<String, String> user = new LinkedHashMap<>(); ArrayList<Pair> inOut = new ArrayList<>(); for (String rec : record) { String[] info = rec.split(" "); char word = rec.charAt(0); if (word == 'E') { user.put(info[1], info[2]); inOut.add(new Pair(word, info[1])); } else if (word == 'L') { inOut.add(new Pair(word, info[1])); } else { user.put(info[1], info[2]); } }
  • Map μ—λŠ” μœ μ €μ•„μ΄λ””, λ‹‰λ„€μž„ μŒμ„ μ €μž₯ν•œλ‹€.
  • ArrayList<Pair> μ—λŠ” μ‚¬μš©μžμ˜ μž…, 퇴μž₯ 여뢀와 ν•΄λ‹Ήν•˜λŠ” μœ μ €μ•„μ΄λ””λ₯Ό μ €μž₯ν•œλ‹€.
  • μž…, 퇴μž₯이 λͺ¨λ‘ λλ‚œ ν›„, μˆœμ„œμ™€ μœ μ €μ•„μ΄λ””μ— 따라 λ‹‰λ„€μž„μ„ 좜λ ₯ν•œλ‹€.

πŸ“„ CODE

μ½”λ“œ 보기/μ ‘κΈ°πŸ’«
import java.util.*; class Pair { char command; String name; public Pair(char command, String name) { this.command = command; this.name = name; } } class Solution { static String in = "λ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€."; static String out = "λ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€."; public String[] solution(String[] record) { // userλŠ” μœ μ €μ•„μ΄λ””μ™€ ν•΄λ‹Ήν•˜λŠ” λ‹‰λ„€μž„μ„ μ €μž₯ // inOut은 μ‚¬μš©μžμ˜ μž…μž₯, 퇴μž₯ 여뢀와 ν•΄λ‹Ήν•˜λŠ” μœ μ €μ•„μ΄λ””λ₯Ό μ €μž₯ Map<String, String> user = new LinkedHashMap<>(); ArrayList<Pair> inOut = new ArrayList<>(); for (String rec : record) { String[] info = rec.split(" "); char word = rec.charAt(0); if (word == 'E') { user.put(info[1], info[2]); inOut.add(new Pair(word, info[1])); } else if (word == 'L') { inOut.add(new Pair(word, info[1])); } else { user.put(info[1], info[2]); } } String[] answer = new String[inOut.size()]; int idx = 0; for (Pair p : inOut) { StringBuilder sb = new StringBuilder(user.get(p.name)); sb.append(p.command == 'E' ? in : out); answer[idx] = sb.toString(); idx++; } return answer; } }

✏️ Review

  • μ‰¬μš΄ λ¬Έμ œμ˜€λ‹€!
@Seongukbaek
배우고 μ •λ¦¬ν•˜κ³  κ³΅μœ ν•˜λŠ” λŒ€ν•™μƒ λ°±μ„±μš±μž…λ‹ˆλ‹€ πŸ˜€